package com.suda.service.impl;

import com.alibaba.fastjson.JSON;
import com.suda.service.WeixinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import entity.HttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WeixinPayServiceImpl implements WeixinPayService {
    @Value("${weixin.appid}")
    private String appid;

    @Value("${weixin.partner}")
    private String partner;

    @Value("${weixin.partnerkey}")
    private String partnerkey;

    @Value("${weixin.notifyurl}")
    private String notifyurl;
    @Override
    public Map createNative(Map<String,String> parameter) {

        try {
            Map parm=new HashMap();
            parm.put("appid", appid);                              //应用ID
            parm.put("mch_id", partner);                           //商户ID号
            parm.put("nonce_str", WXPayUtil.generateNonceStr());   //随机数
            parm.put("body", "suda");                            	//订单描述
            parm.put("out_trade_no",parameter.get("out_trade_no"));                 //商户订单号
            parm.put("total_fee", parameter.get("total_fee"));                      //交易金额
            parm.put("spbill_create_ip", "127.0.0.1");           //终端IP
            parm.put("notify_url", notifyurl);                    //回调地址
            parm.put("trade_type", "NATIVE");                     //交易类型

            parameter.remove("out_trade_no");
            parameter.remove("total_fee");
            parm.put("attach", JSON.toJSONString(parameter));
            String s = WXPayUtil.generateSignedXml(parm, partnerkey);
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            httpClient.setHttps(true);
            httpClient.setXmlParam(s);
            httpClient.post();

            String content = httpClient.getContent();
            Map<String, String> stringStringMap = WXPayUtil.xmlToMap(content);

            System.out.println(stringStringMap);

            Map<String,String> map=new HashMap<>();
            map.put("code_url",stringStringMap.get("code_url"));
            map.put("out_trade_no",parameter.get("out_trade_no"));
            map.put("total_fee",parameter.get("total_fee"));

            return map;
        }catch (Exception e)
        {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    public Map queryPayStatus(String out_trade_no) {

        try {
            Map parm=new HashMap();
            parm.put("appid", appid);                              //应用ID
            parm.put("mch_id", partner);                           //商户ID号
            parm.put("nonce_str", WXPayUtil.generateNonceStr());   //随机数
                              	//订单描述
            parm.put("out_trade_no",out_trade_no);                 //商户订单号
                     //交易类型

            String s = WXPayUtil.generateSignedXml(parm, partnerkey);
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            httpClient.setHttps(true);
            httpClient.setXmlParam(s);
            httpClient.post();

            String content = httpClient.getContent();
            Map<String, String> stringStringMap = WXPayUtil.xmlToMap(content);

            return stringStringMap;
        }catch (Exception e)
        {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    public Map<String, String> closePay(Long orderId) throws Exception {
        //参数设置
        Map<String,String> paramMap = new HashMap<String,String>();
        paramMap.put("appid",appid); //应用ID
        paramMap.put("mch_id",partner);    //商户编号
        paramMap.put("nonce_str",WXPayUtil.generateNonceStr());//随机字符
        paramMap.put("out_trade_no",String.valueOf(orderId));   //商家的唯一编号

        //将Map数据转成XML字符
        String xmlParam = WXPayUtil.generateSignedXml(paramMap,partnerkey);

        //确定url
        String url = "https://api.mch.weixin.qq.com/pay/closeorder";

        //发送请求
        HttpClient httpClient = new HttpClient(url);
        //https
        httpClient.setHttps(true);
        //提交参数
        httpClient.setXmlParam(xmlParam);

        //提交
        httpClient.post();

        //获取返回数据
        String content = httpClient.getContent();

        //将返回数据解析成Map
        return  WXPayUtil.xmlToMap(content);
    }
}
